define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'finance/middleman_performances/index' + location.search,
                    add_url: 'finance/middleman_performances/add',
                    edit_url: 'finance/middleman_performances/edit',
                    // del_url: 'finance/middleman_performances/del',
                    multi_url: 'finance/middleman_performances/multi',
                    import_url: 'finance/middleman_performances/import',
                    table: 'shop',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                // fixedColumns: true,
                // fixedRightNumber: 1,
                searchFormVisible:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: '经纪人ID',operate:false,'visible':false},
                        {field: 'no', title: '经纪人ID'},
                        {field: 'real_name', title: '经纪人名称',operate:false},
                        {field: 'nickname', title: '所属业务员',searchList: Config.salesmanList},
                        {field: 'card_no', title: '银行账户',operate:false},
                        {field: 'rebate_amt', title: '佣金（元/单）',operate:false},
                        {field: 'service_amt', title: '服务费',operate:false},
                        {field: 'shop_name', title: '店铺名称',operate:false},
                        {field: 'wm_order_sn', title: '外卖单号',operate:false},
                        {field: 'place_time', title: '抢单时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'amount', title: '单量',operate:false},
                        {field: 'yc', title: '异常订单（已取消）',operate:false},
                        {field: 'money', title: '金额（元）',operate:false},
                        {field: 'create_dt', title: '到账时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'city', title: '城市组别'},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                        // buttons: [
                            
                        // ]}
                    ]
                ]
            });
            console.log(Config.salesmanList);
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'finance/middleman_performances/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                search:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'delete_dt',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'finance/middleman_performances/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'finance/middleman_performances/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        transfer: function () {
            Controller.api.bindevent();
        },
        middleman_performances: function () {
            Controller.api.bindevent();
            $(document).on("click","#copy-link",function(){
                var url = $('#copycode').text();
                var oInput = document.createElement('input');
                oInput.value = url;
                document.body.appendChild(oInput);
                oInput.select(); // 进择对家
                document.execCommand("Copy"); // 执行浏览器复制命分
                oInput.className = 'oInput';
                oInput.style.display= 'none';
                Layer.msg('复制链接成功:'+url);
                // 为表格绑定事件
            });
        },
        item: function () {
            var ids = $("#assign-data-ids").val();
            var shop_name = $("#shop_name").val();
            var date = $("#date").val();
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                },
                exportTypes: ['excel'],
                exportOptions: {
					fileName: shop_name + ' ' + date,
                    ignoreColumn: [0, 'operate'],
                    onBeforeSaveToFile: function (data, fileName, type, charset, encoding, bom) {
                        return $.fn.bootstrapTable.defaults.extend.savestatus;
                    },
                    onCellHtmlHyperlink: function ($cell, rowIndex, colIndex, href, htmlData) {
                        var html = $.parseHTML(htmlData);
                        var inputidx = 0;
                        var selectidx = 0;
                        var result = '';

                        $.each(html, function () {
                            if ($(this).is("input")) {
                                result += $cell.find('input').eq(inputidx++).val();
                            } else if ($(this).is("select")) {
                                result += $cell.find('select option:selected').eq(selectidx++).text();
                            } else if ($(this).is("img")) {
                                var src = $(this).attr('src');
                                if (src.indexOf('http') === -1) {
                                    $(this).attr('src', window.location.origin + src);
                                }
                                $(this).attr("width", "40").attr("height", "40").attr('class', '');
                                result += '<div>' + $(this).get(0).outerHTML + '</div>';
                            } else {
                                if (typeof $(this).html() === 'undefined') {
                                    result += $(this).text();
                                } else if (jQuery().bootstrapTable === undefined ||
                                    ($(this).hasClass('filterControl') !== true &&
                                        $cell.parents('.detail-view').length === 0)) {
                                    result += $(this).html();
                                }
                            }
                        });
                        return result;
                    }
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'finance/middleman_performances/item/ids/' + ids,
                pk: 'id',
                sortName: 'id',
                search:false,
                searchFormVisible:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'play_order_no', title: '订单编号', operate: false},
                        {field: 'user.nickname', title: '下单用户', operate: false},
                        {field: 'shop.shop_name', title: '店铺名称', operate: false},
                        {field: 'shop.shop_type_text', title: '下单平台', operate: false},
                        {field: 'proof.wm_order_sn', title: '外卖单号', operate: false},
                        {field: 'play.back_money_text2', title: '优惠条件', operate: false},
                        {field: 'proof.actual_pay', title: '实付金额', operate: false},
                        // {field: 'play.service_amt', title: '服务费'},
                        {field: 'play.scheme_no_text', title: '活动方案', operate: false},
                        {field: 'place_time', title: '抢单时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'order_status', title: __('Status'), operate: false, searchList: {"1":"待上传","2":"待审核","3":"已完成","6":"待商家复审","-1":"已取消","-2":"超时取消","-3":"已拒绝",'-5':'不满意反馈','-6':'不满意通过','-7':'异常订单','-8':'异常订单（客服已处理）','-9':'异常订单（已完成）','-4':'异常订单（已取消）'}, formatter: Table.api.formatter.status},
                        {field: 'is_pay', title: '是否结算', searchList: {"0":"未结算","1":"已结算"}, formatter: Table.api.formatter.status},
                        {field: 'proof.one_image', title: '订单截图1', operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'proof.two_image', title: '订单截图2', operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'proof.three_image', title: '订单截图3', operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'create_dt', title: __('Createtime'), operate:'RANGE', operate: false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'update_dt', title: __('Updatetime'), operate:'RANGE', operate: false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
